jQuery(document).ready(function($) {
	
	init();
	
	/*---------- Click Show all step ----------*/
	jQuery("#hfr-show-all-step").click(function () {
		if (showall == 0) {
			showall = 1;
			for (i=0; i < hfrAreas.length; i++) {
				if (step != i+1) {
					showStep(i);
				}
			}
			jQuery(".hfr-panel-btn").show();
		} else {
			showall = 0;
			for (i=0; i < hfrAreas.length; i++) {
				if (step != i+1) {
					hideStep(i);
				}
			}
			if (step != 2) {
				jQuery(".hfr-panel-btn").hide();
			}
		}
	});
	
	/*---------- Check Sitemap Step 1 ----------*/
	jQuery("#hfr-check-sitemap").click(function () {
		var checkSitemap = jQuery(this);
		//Change effect
		if (checkSitemap.attr("disabled") === undefined) {
			
			checkSitemap.attr("disabled", "disabled");
			checkSitemap.parent().find(".hfr-loading").show();
			
			//Check value
			var data = {
				action: 'hfr_check_sitemap',
				sitemap: jQuery("#hfr-sitemap").val()
			};
			
			jQuery.post(hfr_ajax_object.ajax_url, data, function(response) {
				if (response == 0) {
					jQuery(".hfr-loading").hide();
					jQuery(".hfr-message-area").html("<div class=\"hfr-alert-danger\"><strong>Error! </strong>Could not fetch sitemap !</div>");
					checkSitemap.removeAttr("disabled");
				} else {
					step = 2;
					showall = 0;
					showOnlyStep(step);
					checkSitemap.removeAttr("disabled");
					if (response == 1) {
						jQuery("#hfr-type").val("html");
					} else if (response == 2) {
						jQuery("#hfr-type").val("rss");
					} else {
						jQuery("#hfr-type").val("feed");
					}
					jQuery(".hfr-panel-btn").hide();
				}
			});
		}
		
		return false;
	});
	
	/*---------- Check Link Selector Step 2 ----------*/
	jQuery("#hfr-check-link-selector, #hfr-check-link-selector-js").click(function () {
		
		var checkLinkSelector = jQuery(this);
		if (checkLinkSelector.attr("disabled") === undefined) {
		
			checkLinkSelector.attr("disabled", "disabled");
			checkLinkSelector.parent().find(".hfr-loading").show();
			
			var linkSelecterJsValue = jQuery("#hfr-linkSelecterJs").val();
			
			jQuery("#hfr-linkSelecterJs").val(jQuery.base64('encode', linkSelecterJsValue));
			
			//Check value
			var data = {
				action: 'hfr_check_link_selector',
				data: $(".hfr-form-get-content").serialize()
			};
			
			jQuery("#hfr-linkSelecterJs").val(linkSelecterJsValue);
			
			jQuery.get(hfr_ajax_object.ajax_url, data, function(response) {
				if (jQuery.trim(response) != "") {
					step = 3;
					showall = 0;
					showOnlyStep(step);
					jQuery(".hfr-panel-btn").show();
					jQuery("#hfr-example-link").attr("href", response);
					jQuery("#hfr-example-link").html(response);
					checkLinkSelector.removeAttr("disabled");
					jQuery("#hfr-preview").removeAttr("disabled");
					jQuery(".hfr-sample-link").show();
					
				} else {
					jQuery(".hfr-loading").hide();
					jQuery(".hfr-message-area").html("<div class=\"hfr-alert-danger\"><strong>Error! </strong> Link Selecter error!</div>");
					checkLinkSelector.removeAttr("disabled");
				}
			});
		}
		
		return false;
	});
	
	/*---------- Check Preview ----------*/
	jQuery("#hfr-preview").click(function () {
		var hfrPreview = jQuery(this);
		
		if (hfrPreview.attr("disabled") === undefined) {
			jQuery("#hfr-effect-preview").show();
			jQuery(".hfr-content-preview").html("");
			
			var linkSelecterJsValue = jQuery("#hfr-linkSelecterJs").val();
			jQuery("#hfr-linkSelecterJs").val(jQuery.base64('encode', linkSelecterJsValue));
			
			var customSelecterValue = jQuery("#hfr-customSelecter").val();
			jQuery("#hfr-customSelecter").val(jQuery.base64('encode', customSelecterValue));
			
			//Check value
			var data = {
				action: 'hfr_preview',
				data: $(".hfr-form-get-content").serialize()
			};
			
			jQuery("#hfr-linkSelecterJs").val(linkSelecterJsValue);
			jQuery("#hfr-customSelecter").val(customSelecterValue);
			
			jQuery.get(hfr_ajax_object.ajax_url, data, function(response) {
				if(jQuery.trim(response)=="") {
					
				} else {
					jQuery("#hfr-effect-preview").hide();
					jQuery(".hfr-content-preview").html(response);
					jQuery('.hfr-modal-content').animate({ scrollTop: 0 }, 0);
					return true;
				}
			});
		} else {
			return false;
		}
	});
	
	//close js
	/*jQuery("#hfr-link-selecter-js .hfr-close-js, #hfr-custom-selecter-js .hfr-close-js").click(function(){
		jQuery(this).parent().hide("slow");
	});*/
	
	
	/*---------- Check Active Link Js ----------*/
	jQuery("#hfr-active-customer-link-js, #hfr-link-selecter-js .hfr-close-js").click(function () {
		if (jQuery("#hfr-activeLinkJs").val() == 'yes') {
			jQuery("#hfr-activeLinkJs").val("no");
			jQuery("#hfr-link-selecter-js").hide("slow");
			jQuery("#hfr-link-selecter-group").show("slow");
			jQuery("#hfr-linkSelecterJs").attr("disabled", "disabled");
		} else {
			jQuery("#hfr-activeLinkJs").val("yes");
			jQuery("#hfr-link-selecter-js").show("slow");
			jQuery("#hfr-link-selecter-group").hide("slow");
			jQuery("#hfr-linkSelecterJs").removeAttr("disabled");
		}
		
		return false;
	});
	
	/*---------- Check Active Custom Content Js ----------*/
	jQuery("#hfr-active-custom-content-js, #hfr-custom-selecter-js .hfr-close-js").click(function () {
		
		if (jQuery("#hfr-activeContentJs").val() == 'yes') {
			jQuery("#hfr-activeContentJs").val("no");
			jQuery("#hfr-custom-selecter-js").hide("slow");
			jQuery(".hfr-step.step3 .hfr-input-group").show("slow");
			jQuery("#hfr-customSelecter").attr("disabled", "disabled");
		} else {
			jQuery("#hfr-activeContentJs").val("yes");
			jQuery("#hfr-custom-selecter-js").show("slow");
			jQuery(".hfr-step.step3 .hfr-input-group").hide("slow");
			jQuery("#hfr-customSelecter").removeAttr("disabled");
		}
		
		return false;
	});
	
	/*---------- Check when form submit ----------*/
	jQuery(".hfr-form-get-content").submit(function () {
		if (jQuery("#hfr-activeLinkJs").val() == 'yes') {
			var linkSelecterJsValue = jQuery("#hfr-linkSelecterJs").val();
			jQuery("#hfr-linkSelecterJs").val(jQuery.base64('encode', linkSelecterJsValue));
		} else {
			jQuery("#hfr-activeLinkJs").val("no");
		}
		
		if (jQuery("#hfr-activeContentJs").val() == 'yes') {
			var customSelecterValue = jQuery("#hfr-customSelecter").val();
			jQuery("#hfr-customSelecter").val(jQuery.base64('encode', customSelecterValue));
		} else {
			jQuery("#hfr-activeContentJs").val("no");
		}
	});
	
	/*---------- Check Click Event category ----------*/
	jQuery(".hfr-categoryListing .checkbox").change(function () {
		if (jQuery(this).find(".checkcategory").attr('checked')) {
			var name = jQuery(this).find("input").val();
			var catVal = jQuery("#hfr-category").val();
			var categories = catVal != "" ? catVal.split(',') : [];
			
			var exists = false;
			for (var i = 0; i < categories.length; i++) {
				if (categories[i] == name) {
					exists = true;
				}
			}
			
			if (!exists) {
				if (categories.length > 0) {
					categories[categories.length] = name;
				} else {
					categories[0] = name;
				}
				
			}
			
			jQuery("#hfr-category").val(categories.join(","));
			
		} else {
			var name = jQuery(this).find("input").val();
			var catVal = jQuery("#hfr-category").val();
			var categories = catVal.split(',');
			var exists = false;
			var index = 0;
			for (var i = 0; i < categories.length; i++) {
				if (categories[i] == name) {
					exists = true;
					index = i;
				}
			}
			
			if (exists) {
				categories.splice(index,1);
			}
			
			jQuery("#hfr-category").val(categories.join(","));
		}
	});
});

var step = 1;
var showall = 0;
var hfrAreas = ["hfr-step.step1", "hfr-step.step2", "hfr-step.step3"];

init = function () {
	/*---------- tab option ----------*/
	jQuery( "#hfr-tabs" ).tabs();
	
	/*---------- active tooltips ----------*/
	jQuery("[data-toggle='tooltip']").tooltip();
	jQuery("[data-toggle='popover']").popover();
	
	/*---------- init effect ----------*/
	jQuery(".hfr-panel-btn").hide();
	jQuery(".hfr-loading").hide();
	showOnlyStep(step);
	
	
	jQuery(".hfr-sample-link").hide();
	
	//call ace for title selecter js
	var editor = ace.edit("hfr-ace-link-selecter-js");
	editor.setTheme("ace/theme/textmate");
	editor.session.setMode("ace/mode/javascript");
	jQuery("#hfr-linkSelecterJs").val(editor.getSession().getValue());
	editor.getSession().on('change', function(){
		jQuery("#hfr-linkSelecterJs").val(editor.getSession().getValue());
	});
	
	//call ace for custom content js
	var editor1 = ace.edit("hfr-ace-custom-selecter-js");
	editor1.setTheme("ace/theme/textmate");
	editor1.session.setMode("ace/mode/javascript");
	jQuery("#hfr-customSelecter").val(editor1.getSession().getValue());
	editor1.getSession().on('change', function(){
		jQuery("#hfr-customSelecter").val(editor1.getSession().getValue());
	});
	
	
	//Show or hide activeLinkJs
	if (jQuery("#hfr-activeLinkJs").val() == 'yes') {
		jQuery("#hfr-activeLinkJs").val("yes");
		jQuery("#hfr-link-selecter-js").show("slow");
		jQuery("#hfr-link-selecter-group").hide("slow");
		jQuery("#hfr-linkSelecterJs").removeAttr("disabled");
	} else {
		jQuery("#hfr-activeLinkJs").val("");
		jQuery("#hfr-link-selecter-js").hide("slow");
		jQuery("#hfr-link-selecter-group").show("slow");
		jQuery("#hfr-linkSelecterJs").attr("disabled", "disabled");
	}
	
	if (jQuery("#hfr-activeContentJs").val() == 'yes') {
		jQuery("#hfr-activeContentJs").val("yes");
		jQuery("#hfr-custom-selecter-js").show("slow");
		jQuery(".hfr-step.step3 .hfr-input-group").hide("slow");
		jQuery("#hfr-customSelecter").removeAttr("disabled");
	} else {
		jQuery("#hfr-activeContentJs").val("no");
		jQuery("#hfr-custom-selecter-js").hide("slow");
		jQuery(".hfr-step.step3 .hfr-input-group").show("slow");
		jQuery("#hfr-customSelecter").attr("disabled", "disabled");
	}
	
	//Check link category
	var catVal = jQuery("#hfr-category").val();
	var categories = catVal != "" ? catVal.split(',') : [];
	
	for (var i = 0; i < categories.length; i++) {
		jQuery('.hfr-categoryListing li:contains('+categories[i]+')').find("input[type=checkbox]").attr("checked","checked");
	}
}

showStep = function (step) {
	var hfrArea = hfrAreas[step];
	jQuery("."+hfrArea).show("slow");
	jQuery("."+hfrArea).find("a[class*='hfr-btn']").removeAttr("disabled");
	jQuery("."+hfrArea).find(".hfr-loading").hide();
	jQuery(".hfr-message-area").html("");
}

hideStep = function (step) {
	var hfrArea = hfrAreas[step];
	jQuery("."+hfrArea).hide("slow");
	jQuery("."+hfrArea).find("a[class*='hfr-btn']").attr("disabled", "disabled");
	jQuery("."+hfrArea).find(".hfr-loading").hide();
	jQuery(".hfr-message-area").html("");
} 

showOnlyStep = function (currentStep) {
	for (i=0; i < hfrAreas.length; i++) {
		if (i == currentStep - 1) {
			showStep(i);
		} else { 
			hideStep(i);
		}
	}
}